package com.niit.zerotestpdfreader;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.niit.zerotestpdfreader.entity.User;
import com.niit.zerotestpdfreader.service.UserService;


public class RegisterActivity extends AppCompatActivity {
    EditText username;

    EditText password;

    EditText age;

    RadioGroup sex;

    Button register;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_register);

        findViews();

        Intent returnLoginIntent = new Intent(RegisterActivity.this, LoginActivity.class);

        register.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                String name=username.getText().toString().trim();

                String pass=password.getText().toString().trim();

                String agestr=age.getText().toString().trim();

                String sexstr=((RadioButton)RegisterActivity.this.findViewById(sex.getCheckedRadioButtonId())).getText().toString();

                Log.i("TAG",name+"_"+pass+"_"+agestr+"_"+sexstr);

                UserService uService=new UserService(RegisterActivity.this);

                User user=new User();

                user.setUsername(name);

                user.setPassword(pass);

                user.setAge(Integer.parseInt(agestr));

                user.setSex(sexstr);

                uService.register(user);

                Toast.makeText(RegisterActivity.this, "注册成功", Toast.LENGTH_LONG).show();

                startActivity(returnLoginIntent);

            }

        });

    }

    @SuppressLint("WrongViewCast")
    private void findViews() {
        username=(EditText) findViewById(R.id.username);

        password=(EditText) findViewById(R.id.password);

        age=(EditText) findViewById(R.id.age);

        sex=(RadioGroup) findViewById(R.id.sexRegister);

        register=(Button) findViewById(R.id.register);

    }

}
